#include "OLMainWindow.h"
#include "OLToolbar.h"
#include "OLAppTemplateDefs.h"
#include "OLView.h"
#include "OLStatusbar.h"
#include "OLSplashScreen.h"
#include "OLModalBackground.h"

#include <QResizeEvent>
#include <QVBoxLayout>

#ifdef Q_OS_SYMBIAN
    #include <eikenv.h>
    #include <eikappui.h>
    #include <coemain.h>
    #include <aknappui.h>
    #include <aknenv.h>
#endif

//#ifdef Q_OS_LINUX
//#define Q_OS_WIN32
//#endif

OLMainWindow::OLMainWindow(QWidget *parent) :
        QWidget(parent), m_currentOption(OLApplication::systemdefault)
{
#ifdef Q_OS_SYMBIAN
    CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
    if (aknAppUi)
        aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationAutomatic);
#elif defined Q_OS_WIN32
    //m_mainwidget->setFixedSize(APP_WIDTH, APP_HEIGHT);
    resize(APP_WIDTH, APP_HEIGHT);
#elif defined Q_WS_MAEMO_5
    setAttribute(Qt::WA_Maemo5NonComposited); // This will avoid the use of Composite on this Parentless-Widget
    // this should be changed to WA_Maemo5AutoOrientation when supported
    setAttribute(Qt::WA_Maemo5AutoOrientation); // or "Qt::WA_Maemo5LandscapeOrientation" or "Qt::WA_Maemo5AutoOrientation" or "Qt::WA_Maemo5PortraitOrientation"
#endif
    setOrientation(m_currentOption);
}

void OLMainWindow::resizeEvent(QResizeEvent * event)
{
    QWidget::resizeEvent(event);

    int width = event->size().width();
    int height = event->size().height();

    OLApplication::OrientationOptions proposed = m_currentOption;
    if( width>height )
        proposed = OLApplication::landscape;
    else
        proposed = OLApplication::portrait;

    if( m_currentOption!=proposed ) {
        m_currentOption = proposed;
        // the orientation has changed, relayout the screen
        if( proposed==OLApplication::landscape)
            emit handleLandscapeSwitch();
        else if( proposed==OLApplication::portrait )
            emit handlePortraitSwitch();
    }

    emit handleResize();
}

void OLMainWindow::showMainWindow()
{
    #ifdef Q_OS_SYMBIAN
        showFullScreen();
    #elif defined Q_OS_WIN32
        //setWindowFlags(Qt::CustomizeWindowHint);
        show();
    #elif defined Q_WS_MAEMO_5
        showFullScreen();
    #else
        showFullScreen();
    #endif
}

void OLMainWindow::setOrientation(OLApplication::OrientationOptions option)
{
    if( option!=m_systemOption ) {
        if( option==OLApplication::systemdefault ) {
#ifdef Q_OS_SYMBIAN
            CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
            if (aknAppUi)
                aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationAutomatic);
#elif defined Q_WS_MAEMO_5
            setAttribute(Qt::WA_Maemo5AutoOrientation); // this should be changed to WA_Maemo5AutoOrientation when supported
#endif
        }

        if( option==OLApplication::portrait ) {
#ifdef Q_OS_SYMBIAN
            CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
            if (aknAppUi)
                aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationPortrait);
#elif defined Q_OS_WIN32
        setFixedSize(APP_WIDTH, APP_HEIGHT);
        //resize(APP_WIDTH, APP_HEIGHT);
#elif defined Q_WS_MAEMO_5
        setAttribute(Qt::WA_Maemo5PortraitOrientation);
#endif
        }

        if( option==OLApplication::landscape ) {
#ifdef Q_OS_SYMBIAN
            CAknAppUi *aknAppUi = dynamic_cast<CAknAppUi *>(CEikonEnv::Static()->AppUi());
            if (aknAppUi)
                aknAppUi->SetOrientationL(CAknAppUi::EAppUiOrientationLandscape);
#elif defined Q_OS_WIN32
        setFixedSize(APP_HEIGHT, APP_WIDTH);
        //resize(APP_HEIGHT, APP_WIDTH);
#elif defined Q_WS_MAEMO_5
        setAttribute(Qt::WA_Maemo5LandscapeOrientation);
#endif
        }

        m_systemOption = option;
    }
}

OLApplication::OrientationOptions OLMainWindow::orientation() const
{
    return m_currentOption;
}

